﻿//=======================================================
// Author: Jonas Follesø <jonas@follesoe.no>
// Copyright (c) 2008
//
// Licensed under the Microsoft Public License (Ms-PL)
// A copy of the license is available at 
// http://www.opensource.org/licenses/ms-pl.html
//=======================================================

using System.ComponentModel;
using System.ComponentModel.Composition;

using Colorful.Control;
using Colorful.Control.Services;

using Microsoft.Expression.Extensibility;
using Microsoft.Expression.Framework;
using Microsoft.Expression.Framework.Commands;
using Microsoft.Expression.Framework.UserInterface;

namespace Colorful.Blend.AddIn
{
    [Export( typeof (IPackage) )]
    public class ColorCommunityAddIn : CommandTarget, IPackage
    {
        private SearchPanel colorPanel;
        private IMessageLoggingService messageLoggingService;
        private ColorfulViewModel viewModel;
        private IWindowService windowService;

        public void Load( IServices services )
        {
            windowService = services.GetService<IWindowService>();
            messageLoggingService = services.GetService<IMessageLoggingService>();
            Initialize();
        }

        public void Unload()
        {
        }

        public void Initialize()
        {
            viewModel = new ColorfulViewModel( new AdobeKuler(), new CfXamlClipboardFormatter(), new XamlTextClipboardFormatter() );
            viewModel.PropertyChanged += OnViewModelPropertyChanged;
            colorPanel = new SearchPanel( viewModel, true );
            if (windowService.PaletteRegistry["ColorPanel"] == null)
                windowService.RegisterPalette( "ColorPanel", colorPanel, "Colorful Expression - Get colors online" );
        }

        private void OnViewModelPropertyChanged( object sender, PropertyChangedEventArgs e )
        {
            if (e.PropertyName == "ErrorMessage" && viewModel.ErrorMessage != string.Empty)
            {
                messageLoggingService.WriteLine( "An Exception occurred in the Colorful Expression AddIn" );
                messageLoggingService.WriteLine( viewModel.ErrorMessage );
            }
        }
    }
}